﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.VisitionDAL;

namespace thinger.VisionDemo
{
    public partial class FrmLog : Form
    {
        public FrmLog()
        {
            InitializeComponent();
            this.Load += FrmSysLog_Load;

        }

        private void FrmSysLog_Load(object sender, EventArgs e)
        {
       
        }

        private void button3_Click(object sender, EventArgs e)
        {

            //try
            //{
            //    string Sql = "SELECT * FROM sysadmins";
            //    DataTable dataTable = MySqlHelper.GetDataTable(MySqlHelper.Conn, CommandType.Text, Sql);
            //    dgv_Log.DataSource = dataTable;
            //    MessageBox.Show("查询表成功！");
            //}
            //catch (Exception ex)
            //{

            //    MessageBox.Show("查询表失败！" + ex.ToString());

            //}



            //return;


            QueryProcess(this.dtp_Start.Text, this.dtp_End.Text, 1);
        }
        private void QueryProcess(string start, string end, int type)
        {
            //对开始时间和结束时间做判断

            DateTime t_start = Convert.ToDateTime(start);

            DateTime t_end = Convert.ToDateTime(end);

            if (t_start >= t_end)
            {
                MessageBox.Show("开始时间必须小于结束时间", "查询提示");
                return;
            }

            //SQL语句拼接

            StringBuilder sb = new StringBuilder();

            sb.Append("select * from SysLog where LogTime between '{0}' and '{1}'");

            string sql = string.Format(sb.ToString(), start, end);

            if (type <= 1)
            {

                sql += " and LogType=" + type;
            }
            try
            {
                DataTable dataTable = MySqlHelper.GetDataTable(MySqlHelper.Conn, CommandType.Text, sql);
                dataTable.Columns["LogTime"].ColumnName = "记录时间";
                dataTable.Columns["LogType"].ColumnName = "日志类型";
                dataTable.Columns["LogInfo"].ColumnName = "日志内容";
                dataTable.Columns["User"].ColumnName = "操作用户";
                dataTable.Columns["Status"].ColumnName = "记录状态";

                if (dataTable.Rows.Count > 0)
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        row["LogType"] = row["LogType"].ToString() == "1" ? "报警信息" : "日志信息";
                        if (row["LogType"].ToString()=="0")
                        {
                            row["Status"] = row["Status"].ToString() == "1" ? "异常" : "正常";                            
                        }
                        if (row["LogType"].ToString() == "1")
                        {
                            row["Status"] = row["Status"].ToString() == "1" ? "触发" : "消除";
                        }
                    }
                }
                dgv_Log.DataSource = null;
                dgv_Log.Columns.Clear();
                dgv_Log.Rows.Clear();
                dgv_Log.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                //dgv_Log.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

                dgv_Log.DataSource = dataTable;

                MessageBox.Show("查询成功！" + dataTable.Columns[0].Caption);
            }
            catch (Exception)
            {
                MessageBox.Show("查询失败！");
                LogRecord.WriteErrorLog("日志查询失败！" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
         
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "XLS文件(*.xls)|*.xls|所有文件|*.*";

            sfd.FileName = "日志导出";

            sfd.DefaultExt = "xls";

            sfd.AddExtension = true;

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                if (NiceExcelSaveAndRead.SaveToExcelNew(sfd.FileName, this.dgv_Log))
                {
                    MessageBox.Show("报表导出成功！", "报表导出");
                    LogRecord.WriteOperLog("日志报表导出失败!" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                else
                {
                    MessageBox.Show("日志报表导出失败！", "报表导出");
                    LogRecord.WriteErrorLog("日志报表导出失败!" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
            }
        }
    }
}
